package com.superman.supermarket.client.activity;

import java.util.HashMap;
import java.util.Map;

import com.google.gwt.event.shared.EventBus;
import com.google.gwt.user.client.Window;
import com.google.inject.Inject;
import com.superman.mvpframe.client.ajax.PostManAsyncCallBack;
import com.superman.mvpframe.client.event.ReloadEvent;
import com.superman.mvpframe.client.mvp.IPageView;
import com.superman.mvpframe.client.mvp.IPageViewPresenter;
import com.superman.mvpframe.client.utils.XJsonUtil;
import com.superman.supermarket.client.constant.RefundTypeConstants;
import com.superman.supermarket.client.mvp.ShopAbstractActivity;
import com.superman.supermarket.client.utils.ActivityJumpUrl;
import com.superman.supermarket.client.view.refunddetail.RefundDetailView;
import com.superman.supermarket.client.view.refunddetail.RefundDetailView.RefundDetailViewPresenter;

/**
 * 退货申请详情
 * 
 * @author superman
 * @version 2018年8月15日下午5:07:35
 */
public class RefundDetailActivity extends ShopAbstractActivity implements RefundDetailViewPresenter {

	@Inject
	RefundDetailView refundDetailView;
	private String refundId;

	private void getRefundDetail() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("REFUND_ID", refundId);
		postman.post("refund/detail", map, new PostManAsyncCallBack() {

			@Override
			public void onBusinessError(String statusCode) {

			}

			@Override
			public void onBusinessOK(String response) {
				parseRefundDetail(XJsonUtil.getBody(response));
			}
		});
	}

	@Override
	public IPageView getView() {
		return refundDetailView;
	}

	@Override
	public IPageViewPresenter getViewPresenter() {
		return this;
	}

	@Override
	public void onActivity(EventBus eventBus) {
		eventBus.addHandler(ReloadEvent.TYPE, new ReloadEvent.ReloadEventHandler() {

			@Override
			public void reload(ReloadEvent event) {
				getRefundDetail();
			}
		});
	}

	private void parseRefundDetail(Map<String, Object> map) {
		map.put("REFUND_ID", refundId);
		int refundType = (int) Double.parseDouble(String.valueOf(map.get("REFUND_TYPE")));
		if (refundType == RefundTypeConstants.REFUND_ONLY_MONEY) {
			refundDetailView.setPageHeaderText("退款详情");
		} else {
			refundDetailView.setPageHeaderText("退货退款详情");
		}

		refundDetailView.setRefundInfo(map);
	}

	@Override
	public void popToRefresh() {
		// TODO Auto-generated method stub

	}

	@Override
	public void popToRefresh(Object contextData) {
		Map map = (Map) contextData;
		if (map.containsKey("REFUND_ID"))
			getRefundDetail();
	}

	@Override
	public void pushToRefresh() {
		refundId = Window.Location.getParameter("refundid");

		getRefundDetail();
	}

	@Override
	public void pushToRefundProcessPage() {
		ActivityJumpUrl.pushToRefundProcessPage(refundId);
	}

}
